<?php
/**
 * Created by PhpStorm
 * User: honglin
 * Date: 2022/9/2
 * Time: 下午3:07
 */

namespace Form\System;

use App\Dao\System\SystemRolesDao;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\Form\BaseForm;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/system/roles")
 */
class Roles extends BaseForm
{
    public const ADD = true;
    public const EDIT = true;
    public const DELETE = true;
    protected $yjCode = 'system/roles';

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $this->_config();
    }

    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('ID')->setProp('id'),
            Column::txt()->setLabel('管理员名称')->setProp('name'),
            Column::switch()->setLabel('状态')->setProp('status'),
            Column::txt()->setLabel('添加时间')->setProp('create_time')->format(),
            Column::txt()->setLabel('更新时间')->setProp('update_time')->format()
        ]);
        return $colum->toArray();
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getParams()->get('id');
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));


        $form->setRule([
            Elm::input('name', '管理员名称')->required(),
        ]);
        $id && $form->append(
            Elm::hidden('id', $id)
        );
        $lists = $form->setTitle($id ? '编辑' : '添加' . '管理身份')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(SystemRolesDao::class)->where('id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }


}